/*
 * Copyright (C) 2024, Ingenic Semiconductor Co.,Ltd.
 * Author: Keven <keven.ywhan@ingenic.com>
 */

#include <common.h>
#include <ccu.h>
#include <ddr.h>

int ram_test()
{
	unsigned int addr;
	unsigned int w_data;
	unsigned int r_data;

	serial_puts("ram test start\n");
	for(addr = 0x80000000; addr < 0x80001000; addr+=0x100)
	{
		w_data = 0x5a5aa5a5;
		*(volatile unsigned int *)addr = w_data;
	}
	flush_l1cache_all();
	for(addr = 0x80000000; addr < 0x80001000; addr+=0x100)
	{
		r_data = *(volatile unsigned int *)addr;
		if(r_data != w_data) {
			serial_puts("####ram test error addr = %x"); serial_puthex(addr);
			serial_puts("w_data = 0x"); serial_puthex(w_data);
			serial_puts("r_data = 0x"); serial_puthex(r_data);
			return -1;
		}
	}

	serial_puts("ram test end\n");
	return 0;
}


void ram_init()
{
	int ret;
	if(RUNNING_RAM_TYPE == RAM_TYPE_DDR3)
		ddr_init(0);
	else if(RUNNING_RAM_TYPE == RAM_TYPE_DDR4)
		ddr_init(1);

	ret = ram_test();
	if(ret) {
		serial_puts("spl: ram init failed\n");
		while(1);
	}
}

